#ifndef IDX_MRG_H
#define IDX_MRG_H
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <unordered_map>
#include <functional>
#include "sql/Stat.h"
#include "global.h"
using namespace std;

class BPlusTree;

// ========================================
// �������Խӿڲ�
// ========================================

// �������ýṹ
struct IndexConfiguration {
    int attributeOid;
    string indexType;
    bool isUnique;
    bool isActive;
    string creationTime;

    IndexConfiguration(int oid = -1, const string& type = "btree", bool unique = false)
        : attributeOid(oid), indexType(type), isUnique(unique), isActive(true) {
    }
};

// 索引元数据管理器
class IndexMetadataManager {
public:
    static IndexMetadataManager* getInstance();
    static void release();

    void registerIndex(const IndexConfiguration& config);
    void unregisterIndex(int attrOid);
    IndexConfiguration* getIndexConfig(int attrOid);
    vector<IndexConfiguration> getAllConfigs() const;

private:
    static IndexMetadataManager* instance;
    unordered_map<int, IndexConfiguration> configurations;
    IndexMetadataManager() = default;
    ~IndexMetadataManager() = default;
    IndexMetadataManager(const IndexMetadataManager&) = delete;
    IndexMetadataManager& operator=(const IndexMetadataManager&) = delete;
};

// �����������Խӿ�
class IIndexStrategy {
public:
    virtual ~IIndexStrategy() = default;
    virtual bool createIndex(int attrOid) = 0;
    virtual bool hasIndex(int attrOid) const = 0;
    virtual bool addRecord(int attrOid, ment::Stat* stat, const string& rid) = 0;
    virtual bool removeRecord(int attrOid, ment::Stat* stat) = 0;
    virtual vector<BPTDataType> queryRecords(int attrOid) = 0;
    virtual vector<BPTDataType> queryRecords(int attrOid, const vector<ment::Stat*>& stats) = 0;
    virtual vector<BPTDataType> queryRecords(int attrOid, ment::Stat* stat, COMPARE_OPERATOR cmp) = 0;
    virtual void clearIndex(int attrOid) = 0;
    virtual string getStrategyName() const = 0;
};

// B+����������ʵ��
class BPlusTreeIndexStrategy : public IIndexStrategy {
public:
    BPlusTreeIndexStrategy();
    virtual ~BPlusTreeIndexStrategy();

    bool createIndex(int attrOid) override;
    bool hasIndex(int attrOid) const override;
    bool addRecord(int attrOid, ment::Stat* stat, const string& rid) override;
    bool removeRecord(int attrOid, ment::Stat* stat) override;
    vector<BPTDataType> queryRecords(int attrOid) override;
    vector<BPTDataType> queryRecords(int attrOid, const vector<ment::Stat*>& stats) override;
    vector<BPTDataType> queryRecords(int attrOid, ment::Stat* stat, COMPARE_OPERATOR cmp) override;
    void clearIndex(int attrOid) override;
    string getStrategyName() const override { return "BPlusTree"; }

private:
    map<int, BPlusTree*> indexTrees;
    BPlusTree* getIndexTree(int attrOid);
    BPTKeyType generateIndexKey(ment::Stat* stat);
};

// 索引策略工厂
class IndexStrategyFactory {
public:
    using StrategyCreator = function<unique_ptr<IIndexStrategy>()>;

    static IndexStrategyFactory* getInstance();
    static void release();

    void registerStrategy(const string& strategyName, StrategyCreator creator);
    unique_ptr<IIndexStrategy> createStrategy(const string& strategyName);
    vector<string> getAvailableStrategies() const;

private:
    static IndexStrategyFactory* instance;
    unordered_map<string, StrategyCreator> strategyCreators;
    IndexStrategyFactory();
    ~IndexStrategyFactory() = default;
    IndexStrategyFactory(const IndexStrategyFactory&) = delete;
    IndexStrategyFactory& operator=(const IndexStrategyFactory&) = delete;
};

// ========================================
// �߼�����������
// ========================================
class AdvancedIndexManager {
public:
    static AdvancedIndexManager* getInstance();
    static void release();
    ~AdvancedIndexManager();

    // �߼����������ӿ�
    bool createIndexWithStrategy(int attrOid, const string& strategyName = "BPlusTree");
    bool dropIndex(int attrOid);
    bool rebuildIndex(int attrOid);

    // 索引操作接口
    bool addIndexRecord(int attrOid, ment::Stat* stat, const string& rid);
    bool removeIndexRecord(int attrOid, ment::Stat* stat);

    // 查询接口
    vector<BPTDataType> executeQuery(int attrOid);
    vector<BPTDataType> executeQuery(int attrOid, const vector<ment::Stat*>& stats);
    vector<BPTDataType> executeQuery(int attrOid, ment::Stat* stat, COMPARE_OPERATOR cmp);

    // 索引状态查询
    bool indexExists(int attrOid) const;
    IndexConfiguration* getIndexInfo(int attrOid);
    vector<IndexConfiguration> getAllIndexInfo() const;

private:
    static AdvancedIndexManager* instance;
    unordered_map<int, unique_ptr<IIndexStrategy>> activeStrategies;

    AdvancedIndexManager();
    AdvancedIndexManager(const AdvancedIndexManager&) = delete;
    AdvancedIndexManager& operator=(const AdvancedIndexManager&) = delete;

    IIndexStrategy* getStrategy(int attrOid);
};

class IdxMgr
{
public:
    static IdxMgr* getInstance();
    static void release();
    ~IdxMgr();

    bool hasIdx(int attrOid);		//��ѯһ������һ�������Ƿ�������
    void createIdx(int attrOid);	//Ϊһ�����ϵ�һ�д�������
    void addRecord(int attrOid, ment::Stat* stat, string rid);	//Ϊ��������һ����¼
    void removeRecord(int attrOid, ment::Stat* stat);
    vector<BPTDataType> getRowids(int attrOid, vector< ment::Stat*> Stat);
    vector<BPTDataType> getRowids(int attrOid);
    vector<BPTDataType> getRowids(int attrOid, ment::Stat* stat, COMPARE_OPERATOR cmp);

private:
    static IdxMgr* instance;
    AdvancedIndexManager* advancedManager; 

    IdxMgr();
    IdxMgr(const IdxMgr&) = delete;
    IdxMgr& operator=(const IdxMgr&) = delete;

    BPlusTree* getBPT(int attrOid);
    BPTKeyType generateKey(ment::Stat* stat);

    map<int, BPlusTree*> idx;  
};


#endif // !IDX_MRG_H